package com.oyjd.ybx.business.role.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.oyjd.ybx.bean.po.Role;
import com.oyjd.ybx.bean.po.RoleOperate;
import com.oyjd.ybx.bean.po.User;
import com.oyjd.ybx.bean.vo.RoleOperateVO;
import com.oyjd.ybx.bean.vo.RoleVO;
import com.oyjd.ybx.business.role.dao.RoleDao;
import com.oyjd.ybx.business.role.service.RoleService;
import com.oyjd.ybx.business.role.vo.SysOperateExVO;
import com.oyjd.ybx.business.sys.service.SysService;
import com.oyjd.ybx.fw.action.Page;

/**
 * 
 * 角色业务接口实现
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Component
@Transactional(readOnly = true)
public class RoleServiceImpl implements RoleService {
	private RoleDao dao;

	@Autowired
	public void setDao(RoleDao dao) {
		this.dao = dao;
	}

	private SysService sysService;

	@Autowired
	public void setSysService(SysService sysService) {
		this.sysService = sysService;
	}

	/**
	 * 获取角色列表数据
	 * 
	 * @param page
	 *            页面对象
	 * @return
	 */
	public Page<RoleVO> getList(Page<RoleVO> page) {
		return dao.getList(page);
	}

	/**
	 * 获取空白模块分组操作
	 * 
	 * @return
	 */
	public Map<String, List<SysOperateExVO>> getOps() {
		return dealSysOperateExVO(dao.getOps());
	}

	/**
	 * 根据角色ID获取模块分组操作
	 * 
	 * @param roleId
	 *            角色ID
	 * @return
	 */
	public Map<String, List<SysOperateExVO>> getOpsByRoleId(String roleId) {
		List<SysOperateExVO> tmp1 = dao.getOps();
		List<RoleOperateVO> tmp2 = dao.getSysOps(roleId);
		//
		for (SysOperateExVO so : tmp1) {
			for (RoleOperateVO ro : tmp2) {
				if (so.getOperateId().equals(ro.getOperateId())) {
					so.setChecked(true);
					break;
				}
			}
		}
		return dealSysOperateExVO(tmp1);
	}

	/**
	 * 按模块名处理成map型
	 * 
	 * @param tmp
	 * @return
	 */
	private Map<String, List<SysOperateExVO>> dealSysOperateExVO(List<SysOperateExVO> tmp) {
		Map<String, List<SysOperateExVO>> result = new LinkedHashMap<String, List<SysOperateExVO>>();
		for (SysOperateExVO vo : tmp) {
			String key = vo.getModuleName();
			if (result.containsKey(key)) {
				result.get(key).add(vo);
			} else {
				List<SysOperateExVO> list = new ArrayList<SysOperateExVO>();
				list.add(vo);
				result.put(key, list);
			}
		}
		return result;
	}

	/**
	 * 根据角色ID获取角色信息
	 * 
	 * @param roleId
	 *            角色ID
	 * @return
	 */
	public RoleVO getRole(String roleId) {
		return dao.getRole(roleId);
	}

	/**
	 * 增加角色
	 * 
	 * @param name
	 *            角色名称
	 * @param description
	 *            角色描述
	 * @param ops
	 *            操作列表
	 * @param user
	 *            当前用户
	 */
	@Transactional(readOnly = false)
	public void add(String name, String description, String[] ops, String user) {
		Date date = new Date();
		Role role = new Role();
		role.setName(name);
		role.setDescription(description);
		role.setCreator(user);
		role.setCreateTime(date);
		role.setLastEditor(user);
		role.setLastUpdateTime(date);
		role.setStatus(0);
		dao.create(role);
		//
		String roleId = role.getRoleId();
		for (String opId : ops) {
			RoleOperate roleOperate = new RoleOperate();
			roleOperate.setRoleId(roleId);
			roleOperate.setOperateId(opId);
			dao.create(roleOperate);
		}
		//
		sysService.init();
	}

	/**
	 * 修改角色
	 * 
	 * @param roleId
	 *            角色ID
	 * @param name
	 *            角色名称
	 * @param description
	 *            角色描述
	 * @param ops
	 *            操作列表
	 * @param user
	 *            当前用户
	 */
	@Transactional(readOnly = false)
	public void edit(String roleId, String name, String description, String[] ops, String user) {
		Date date = new Date();
		Role role = (Role) dao.queryById(Role.class, roleId);
		role.setName(name);
		role.setDescription(description);
		role.setLastEditor(user);
		role.setLastUpdateTime(date);
		dao.update(role);
		//
		dao.executeHql("delete from " + RoleOperate.class.getName() + " where roleId=?", new Object[] { roleId });
		for (String opId : ops) {
			RoleOperate roleOperate = new RoleOperate();
			roleOperate.setRoleId(roleId);
			roleOperate.setOperateId(opId);
			dao.create(roleOperate);
		}
		//
		sysService.init();
	}

	/**
	 * 删除角色
	 * 
	 * @param roleId
	 *            角色ID
	 * @param user
	 *            当前用户
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@Transactional(readOnly = false)
	public boolean remove(String roleId, String user) {
		boolean flag = false;
		List t = dao.queryByHql("from " + User.class.getName() + " where roleId=?", new Object[] { roleId });
		if (t.size() == 0) {
			Role role = (Role) dao.queryById(Role.class, roleId);
			role.setStatus(1);
			role.setLastEditor(user);
			role.setLastUpdateTime(new Date());
			dao.update(role);
			//
			sysService.init();
		}
		return flag;
	}

}
